package com.uinnova.product.eam.web.cj;

import com.binary.core.util.BinaryUtils;
import com.uinnova.product.eam.base.cj.ResultMsg;
import com.uinnova.product.eam.base.enums.ResultCodeEnum;
import com.uinnova.product.eam.model.cj.domain.ChapterContext;
import com.uinnova.product.eam.model.cj.request.ChapterContextRequest;
import com.uinnova.product.eam.model.cj.request.DeleteModulesRequest;
import com.uinnova.product.eam.model.cj.request.UpdateVersionRequest;
import com.uinnova.product.eam.model.cj.vo.ChapterContextVO;
import com.uinnova.product.eam.service.cj.service.ChapterContextService;
import com.uinnova.product.eam.service.utils.XssCleanerUtil;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.xss.core.XssType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;


/**
 * 方案章节详情 controller
 *
 * @author zhaoxin
 */
@RestController
@RequestMapping("/chapter/context")
@Slf4j
public class ChapterContextController {

    @Resource
    private ChapterContextService chapterContextService;


    /**
     * 方案章节新增
     */
    @PostMapping("save")
    public ResultMsg save(@RequestBody @Validated ChapterContextRequest request) {
        //对章节说明进行xss过滤
        if(!BinaryUtils.isEmpty(request.getChapterDesc())){
            log.debug("过滤xss: " + request.getChapterDesc());
            XssCleanerUtil xssCleaner = new XssCleanerUtil();
            String cleanStr = xssCleaner.clean("", request.getChapterDesc(), XssType.JACKSON);
            log.debug("过滤结果: " + cleanStr);
            request.setChapterDesc(cleanStr);
        }
        Long result = chapterContextService.save(request);
        if (Objects.equals(result, -1L)) {
            return new ResultMsg(false, ResultCodeEnum.APPROVE_ERROR.getCode(), "该方案已提交审批，请刷新后重试!");
        } else if (Objects.equals(result, -2L)){
            return new ResultMsg(false, 404, "暂无权限!");
        } else {
            return new ResultMsg(result);
        }
    }

    /**
     * 查找
     *
     * @param chapterId 章节id
     */
    @GetMapping
    public ResultMsg findById(Long chapterId) {

        ChapterContextVO byId = chapterContextService.findById(chapterId);
        return new ResultMsg(byId);
    }

    @GetMapping("/findIntroduceContextById")
    public ResultMsg findIntroduceContextById(Long introduceChapterId) {
        ChapterContext context = chapterContextService.findIntroduceContextById(introduceChapterId);
        return new ResultMsg(context);
    }


    /**
     * 批量删除模块
     *
     * @param request {@link DeleteModulesRequest}
     * @return ResultMsg
     */
    @PostMapping("/module/delete")
    public ResultMsg delModule(@RequestBody @Validated DeleteModulesRequest request) {
        chapterContextService.delModule(request);
        return ResultMsg.ok();
    }

    /**
     * 全量更新方案内容框视图信息版本为最新版本
     */
    @GetMapping("/updateAllContextDiagramVersion")
    public ResultMsg updateAllContextDiagramVersion(@RequestParam Long planId) {
        chapterContextService.updateAllContextDiagramVersion(planId);
        return ResultMsg.ok();
    }

    /**
     * 更新单个内容块视图最新版本 在画布里面
     */
    @PostMapping("/updateSigleDiagramVersion")
    public ResultMsg updateSingleDiagramVersion(@RequestBody UpdateVersionRequest updateVersionRequest) {
        chapterContextService.updateSingleDiagramVersion(updateVersionRequest);
        return ResultMsg.ok();
    }
}

